package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.tencent.upload.task.ITask;
import com.tencent.upload.task.IUploadTaskListener;
import com.tencent.upload.task.data.FileInfo;
import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.network.TXYUploadManager;
import com.youmingxing.view.TextCountWatcher;

import org.json.JSONObject;

import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;


/**
 * Created by JHS on 2015/9/10
 * 实名认证
 */
public class CertificationActivity extends BaseActivity {


    private EditText nameEd;
    private EditText idEdt;
    private TextView imgText;
    private ImageView image;
    private String imgUrl = "";
    private String name;
    private String id;
    private Handler handler = new Handler();
    private Button uploadBtn;

    boolean idChecked = false;
    boolean nameChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_certification);
        initView();
    }

    private void initView() {
        setTopbarName(R.string.certificationcitle);
        nameEd = (EditText) findViewById(R.id.name);
        idEdt = (EditText) findViewById(R.id.certification_id_edt);
        TextView uploadImg = (TextView) findViewById(R.id.certification_uploadimg);
        uploadBtn = (Button) findViewById(R.id.certification_btn);
        imgText = (TextView) findViewById(R.id.certification_img);
        image = (ImageView) findViewById(R.id.certification_img_img);

        uploadBtn.setOnClickListener(listener);
        uploadImg.setOnClickListener(imageListener);
        notClick();
        nameEd.addTextChangedListener(new TextCountWatcher(1) {
            @Override
            public void onTextMatchCount() {
                nameChecked = true;
                if (idChecked && !imgUrl.equals("")) {
                    canClick();
                }
            }

            @Override
            public void onTextLessThanCount() {
                nameChecked = false;
                notClick();
            }
        });
        idEdt.addTextChangedListener(new TextCountWatcher(15) {
            @Override
            public void onTextMatchCount() {
                idChecked = true;
                if (nameChecked && !imgUrl.equals("")) {
                    canClick();
                }
            }

            @Override
            public void onTextLessThanCount() {
                idChecked = false;
                notClick();
            }
        });
    }

    View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name = nameEd.getText().toString();
            id = idEdt.getText().toString();
            if (name.equals("")) {
                showToast("请输入真实姓名");
                return;
            }
            if (id.equals("")) {
                showToast("请输入身份证号");
                return;
            }
            if (imgUrl.equals("")) {
                showToast("请上传照片");
                return;
            }
            showDialog("提交信息中");
            SelfInfoApi.realNameAuth(name, id, imgUrl, confirmHandler);
        }
    };

    BaseJsonHandler confirmHandler = new BaseJsonHandler(this){
        @Override
        public void onSuccessWithData(JSONObject response) {
            showToast("提交成功");
            hideDialog();
            Intent intent = new Intent(CertificationActivity.this, CertificationingActivity.class);
            intent.putExtra("name", name)
                    .putExtra("id", id)
                    .putExtra("img_url", imgUrl);
            startActivity(intent);
            finish();
        }
    };

    View.OnClickListener imageListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PhotoPickerIntent intentPP = new PhotoPickerIntent(CertificationActivity.this);
            intentPP.setPhotoCount(1);
            intentPP.setShowCamera(true);
            intentPP.setShowGif(false);
            startActivityForResult(intentPP, 0);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            showDialog("图片上传中");
            final String photoPath = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS).get(0);
            TXYUploadManager.uploadPhoto(photoPath, new IUploadTaskListener() {
                @Override
                public void onUploadSucceed(FileInfo fileInfo) {
                    Toast.makeText(CertificationActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                    imgUrl = fileInfo.url;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            imgText.setVisibility(View.INVISIBLE);
                            image.setVisibility(View.VISIBLE);
                            Glide.with(CertificationActivity.this).load(photoPath).into
                                    (image);
                            if (idChecked && nameChecked) {
                                canClick();
                            }
                        }
                    });
                    hideDialog();
                }

                @Override
                public void onUploadFailed(int i, String s) {
                    hideDialog();
                    Toast.makeText(CertificationActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onUploadProgress(long l, long l1) {

                }

                @Override
                public void onUploadStateChange(ITask.TaskState taskState) {

                }
            });
        }
    }
    private void canClick() {
        uploadBtn.setBackgroundResource(R.drawable.btn_redbg);
        uploadBtn.setClickable(true);
    }

    private void notClick() {
        uploadBtn.setBackgroundResource(R.drawable.btn_unable);
        uploadBtn.setClickable(false);
    }
}